package com.jzo2o.orders.manager.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.market.CouponApi;
import com.jzo2o.api.market.dto.request.CouponUseBackReqDTO;
import com.jzo2o.api.market.dto.request.CouponUseReqDTO;
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
import com.jzo2o.api.market.dto.response.CouponDto;
import com.jzo2o.api.market.dto.response.CouponUseResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * TODO
 *
 * @Author lyh
 * @Date 2024/12/25 10:57
 */
@Component
@Slf4j
public class MarkerClient {
    @Autowired
    private CouponApi couponApi;
    @SentinelResource(value = "getAvailable", fallback = "getAvailableFallback", blockHandler = "getAvailableBlockHandler")
    public List<AvailableCouponsResDTO> getAvailable(BigDecimal totalAmount){
        List<AvailableCouponsResDTO> dtos = couponApi.getAvailable(totalAmount);
        return dtos;
    }
    //执行异常走
    public List<AvailableCouponsResDTO> getAvailableFallback(BigDecimal totalAmount, Throwable throwable) {
        log.error("非限流、熔断等导致的异常执行的降级方法，totalAmount:{},throwable:", totalAmount, throwable);
        return null;
    }

    //熔断后的降级逻辑
    public List<AvailableCouponsResDTO> getAvailableBlockHandler(BigDecimal totalAmount, BlockException blockException) {
        log.error("触发限流、熔断时执行的降级方法，totalAmount:{},blockException:", totalAmount, blockException);
        return null;
    }
    @SentinelResource(value = "use", fallback = "useFallback", blockHandler = "useBlockHandler")
    public CouponUseResDTO use(CouponUseReqDTO dto){
        CouponUseResDTO use = couponApi.use(dto);
        return use;
    }
    //执行异常走
    public  CouponUseResDTO useFallback(CouponUseReqDTO dto, Throwable throwable) {
        log.error("非限流、熔断等导致的异常执行的降级方法，dto:{},throwable:", dto, throwable);
        return null;
    }

    //熔断后的降级逻辑
    public  CouponUseResDTO useBlockHandler(CouponUseReqDTO dto, BlockException blockException) {
        log.error("触发限流、熔断时执行的降级方法，dto:{},blockException:", dto, blockException);
        return null;
    }
    @SentinelResource(value = "useBack", fallback = "useBackFallback", blockHandler = "useBackBlockHandler")
    public void useBack(CouponUseBackReqDTO dto){
        couponApi.useBack(dto);
    }
    //执行异常走
    public  void useBackFallback(CouponUseBackReqDTO dto, Throwable throwable) {
        log.error("非限流、熔断等导致的异常执行的降级方法，dto:{},throwable:", dto, throwable);
    }

    //熔断后的降级逻辑
    public  void useBackBlockHandler(CouponUseBackReqDTO dto, BlockException blockException) {
        log.error("触发限流、熔断时执行的降级方法，dto:{},blockException:", dto, blockException);
    }
    @SentinelResource(value = "getCouponByOrdersId", fallback = "getCouponByOrdersIdFallback", blockHandler = "getCouponByOrdersIdBlockHandler")
    public CouponDto getCouponByOrdersId(Long ordersId){
        CouponDto coupon = couponApi.getCouponByOrdersId(ordersId);
        return coupon;
    }
    //执行异常走
    public  CouponDto getCouponByOrdersIdFallback(Long ordersId, Throwable throwable) {
        log.error("非限流、熔断等导致的异常执行的降级方法，ordersId:{},throwable:", ordersId, throwable);
        return null;
    }

    //熔断后的降级逻辑
    public  CouponDto getCouponByOrdersIdBlockHandler(Long ordersId, BlockException blockException) {
        log.error("触发限流、熔断时执行的降级方法，ordersId:{},blockException:", ordersId, blockException);
        return null;
    }

}
